package com.columboid.protocol.syncml.tests;

import java.io.StringReader;

import javax.xml.bind.JAXBException;

import org.junit.Test;

import com.columboid.protocol.syncml.helper.JaxbHelper;
import com.columboid.protocol.syncml.metainfo.Format;
import com.columboid.protocol.syncml.metainfo.MetInf;
import com.columboid.protocol.syncml.metainfo.NextNonce;
import com.columboid.protocol.syncml.metainfo.ObjectFactory;
import com.columboid.protocol.syncml.metainfo.Type;
import com.columboid.protocol.syncml.representation.Chal;
import com.columboid.protocol.syncml.tests.resources.SyncMLString;

import junit.framework.TestCase;

public class MetaInfoTest extends TestCase {

	@Test
	public void testMetaParsing() throws JAXBException {
		String xml = SyncMLString.getMetaObject();
		String xsd = JaxbHelper.getMetaSchema();

		StringReader xmlReader = new StringReader(xml);
		StringReader xsdReader = new StringReader(xsd);
		try {

			MetInf sml = JaxbHelper.CreateSyncMLObject(xmlReader, xsdReader,
					MetInf.class,ObjectFactory.class);

			assertTrue(sml.getNextNonce().getContent().equals("12345"));

		} catch (Exception ex) {
			System.out.println(ex.getMessage());
			assertTrue(false);
		} finally {
			if (xmlReader != null) {
				xmlReader.close();
				xmlReader = null;
			}

			if (xsdReader != null) {
				xsdReader.close();
				xsdReader = null;
			}

		}
	}
	
	@Test
	public void testMetaStringGeneration() throws JAXBException {
		
		String xml = SyncMLString.getMetaObject();
		
		
		MetInf meta = new MetInf();
		Format format= new Format();
		format.setContent("b64");
		meta.setFormat(format);
		
		Type type= new Type();
		type.setContent("syncml:auth-md5");
		meta.setType(type);
		
		NextNonce nextNonce= new NextNonce();
		nextNonce.setContent("12345");
		meta.setNextNonce(nextNonce);
		
		String metaString=JaxbHelper.getJaxbObjectString(meta, MetInf.class,false);
		assertTrue(xml.equals(metaString));
		
		
		
	}
}
